/**
 * 
 */
package com.feib.stms.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.feib.stms.dao.BusinessDayDao;
import com.feib.stms.model.BusinessDay;
import com.feib.stms.service.BusinessDayService;

/**
 * @author Lion Gao
 *
 */
@Service("businessDayService")
public class BusinessDayServiceImpl extends BaseServiceImpl implements BusinessDayService 
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 2223208623467776876L;
	
	@Resource(name="businessDayDao")
    private BusinessDayDao businessDayDao;
	
	/*
	 * (non-Javadoc)
	 * @see com.feib.stms.service.BusinessDayService#getBusinessDayByDate(String, String)
	 */
	public List<BusinessDay> getBusinessDayByDate(String year, String month) {
		return businessDayDao.getBusinessDayByDate(year, month);
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.feib.stms.service.BusinessDayService#insertBusinessDay(java.lang.String, java.lang.String, java.util.List)
	 */
	public int insertBusinessDay(String year , String month, List<BusinessDay> checkBusinessDay) {
		int returnCount = 0;
		try{
			SimpleDateFormat sf = new SimpleDateFormat("yyyy/MM/dd");
			Calendar workCal = Calendar.getInstance();
			workCal.set(Integer.parseInt(year), Integer.parseInt(month) - 1, 1);
			
			//先刪后增
			//刪除
			int deleteResult = businessDayDao.delete(year , month);
			if(deleteResult > 0){
				//新增(新增工作日)
				if(checkBusinessDay != null && checkBusinessDay.size() > 0){
					for(BusinessDay bd:checkBusinessDay){
						if(bd != null && bd.getDay() != null && bd.getStatus() != null && !"".equals(bd.getStatus())){
							Date now = sf.parse(year + "/" + month + "/" + bd.getDay());
							BusinessDay businessDay = new BusinessDay();
							businessDay.setTheDate(now);
							businessDay.setStatus("W");
							returnCount = businessDayDao.insert(businessDay);
						}
						
					}
				}
				//新增(新增假日)
				List<String> workDayList = new ArrayList<String>();
				if(checkBusinessDay != null && checkBusinessDay.size() > 0){
					for(int i = 0;i < checkBusinessDay.size(); i++){
						BusinessDay bd = checkBusinessDay.get(i);
						if(bd != null && bd.getDay() != null && bd.getStatus() != null && !"".equals(bd.getStatus())){
							workDayList.add(bd.getDay() + "");
						}				
					}
				}
				//獲得當前月所有天數
				List<String> allDayList = new ArrayList<String>();
				for (int i = 1; i <= workCal.getActualMaximum(Calendar.DAY_OF_MONTH); i++) {//循環當前月的所有天數
					allDayList.add(i + "");
				}
				//移除工作日
				allDayList.removeAll(workDayList);
				//新增
				for(String i:allDayList){
					Date now = sf.parse(year + "/" + month + "/" + i);
					BusinessDay businessDay = new BusinessDay();
					businessDay.setTheDate(now);
					businessDay.setStatus("F");
					returnCount = businessDayDao.insert(businessDay);
				}
			}
		}catch(Exception e){
			logger.error(e.getMessage());
		}
		return returnCount;
	}
	
	public Date getNextBusinessDay(Date day){
		return businessDayDao.getNextBusinessDay(day);
	}
	
}
